package fr.bugtracker.bean.security;

import java.io.IOException;

import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import fr.bugtracker.commun.enumeration.EnumParametreApplication;
import fr.bugtracker.commun.outils.UtilsAuthentication;
import fr.bugtracker.commun.outils.UtilsJsf;
import fr.bugtracker.entity.technique.Utilisateur;

/**
 *
 * @author Laurent
 * @version 0.1
 */
public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

	/**
	 * @see org.springframework.security.web.authentication.AuthenticationSuccessHandler#onAuthenticationSuccess(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.security.core.Authentication)
	 */
	@Override
	public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
		UtilsJsf.changeSessionTimeout(Integer.valueOf(UtilsJsf.getBeanApplication().getMapParametre().get(EnumParametreApplication.session_timeout)));
		UtilsAuthentication.updateUtilisateur((Utilisateur)((UsernamePasswordAuthenticationToken) authentication).getPrincipal());
		UtilsJsf.doRedirect(FacesContext.getCurrentInstance(), "/faces/pages/private/accueil.xhtml");
	}

}
